package com.ride.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.ride.dao.AddressDao;
import com.ride.model.Address;
import org.springframework.stereotype.Repository;

/**
 *
 * @version $LastChangedRevision: 16 $
 *          <p/>
 *          $LastChangedBy: marcel.menze@gmail.com $
 *          <p/>
 *          $LastChangedDate: 2011-04-05 12:34:26 +0000 (Tue, 05 Apr 2011) $
 */
@Repository
public class AddressDaoImpl implements AddressDao {

    @PersistenceContext(name = "ride")
    private EntityManager em;

    public AddressDaoImpl() {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void persist(Address address) {
        em.persist(address);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Address findById(Long id) {
        return em.find(Address.class, id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void update(Address address) {
        em.merge(address);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void delete(Address address) {
        em.remove(address);
    }

    /**
     * {@inheritDoc}
     */
    public List<Address> findAllAddresses() {
        List<Address> addresses = em.createQuery("select a from Address a").getResultList();
        return addresses;
    }

    /**
     * {@inheritDoc}
     */
    public List<Address> findAddress(String street, int number, String city, int zipCode) {
        List<Address> addresses = em.createQuery(
                "select a from Address a where a.street = :street and a.streetNumber = :number and a.city = :city and a.zipCode = :zipCode").
                setParameter("street", street).setParameter("number", number).setParameter("city", city).setParameter("zipCode", zipCode).
                getResultList();
        return addresses;
    }
}
